<?php
/* 礼品类
 * @author  zhanghui
 * @create_date 2008-02-29
 */
class BabyItem{
	function __construct(){}
	function __destruct(){}
	
	/* 添加礼品 
	* @prame: conn 数据库连接类
	* @prame: array $arr_input - baby_id,item_id,seq,get_type,sender_id,sender_name,sender_message,create_date  
	*/
	function addBabyItem($conn,$arr_input){
		$str_query = "insert into";
		$str_query .= " baby_item";
		$str_query .= " set";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$str_query .= ",item_id={$arr_input['item_id']}";
		$str_query .= ",get_type={$arr_input['get_type']}";
		if(isset($arr_input['sender_id'])){
			$str_query .= ",sender_id={$arr_input['sender_id']}";
		}
		if(isset($arr_input['sender_name'])){
			$str_query .= ",sender_name='{$arr_input['sender_name']}'";
		}
		if(isset($arr_input['sender_message'])){
			$str_query .= ",sender_message='{$arr_input['sender_message']}'";
		}
        $str_query .= ",create_date='".$conn->getCurrentTime()."'";
        
		$conn->executeQuery($str_query);
	}
	
	/* 发送礼品 
	* @prame: conn 数据库连接类
	* @prame: array $arr_input - baby_id,item_id,seq,get_type,sender_id,sender_name,sender_message,create_date  
	*/
	function sendBabyItem($conn,$arr_input){
		/* 自购礼品 */
		if($arr_input['get_type'] == 1){
			$arr_tmp_input['baby_id'] = $arr_input['baby_id'];
		    $arr_tmp_input['item_id'] = $arr_input['item_id'];
		    $this->addBabyItem($conn,$arr_tmp_input);
		}
		
		/* 赠送礼品 */
		if($arr_input['get_type'] == 2){
		    $arr_tmp_input['baby_id'] = $arr_input['sender_id'];
		    $arr_tmp_input['item_id'] = $arr_input['item_id'];
		    $arr_tmp_input['sender_id'] = $arr_input['baby_id'];
		    $arr_tmp_input['sender_name'] = $arr_input['sender_name'];
		    $arr_tmp_input['sender_message'] = $arr_input['sender_message'];
		    $this->addBabyItem($conn,$arr_tmp_input);
		}
		
		
		/* 系统礼品 */
		if($arr_input['get_type'] == 3){
		    $arr_tmp_input['baby_id'] = $arr_input['sender_id'];
		    $arr_tmp_input['item_id'] = $arr_input['item_id'];
		    $arr_tmp_input['sender_id'] = $arr_input['baby_id'];
		    $arr_tmp_input['sender_name'] = $arr_input['sender_name'];
		    $arr_tmp_input['sender_message'] = $arr_input['sender_message'];
		    $this->addBabyItem($conn,$arr_tmp_input);
		}
		
	}
	
	/* 删除礼品 
	* @prame: conn 数据库连接类
	* @prame: array $arr_input - baby_id,item_id,seq,get_type,sender_id,sender_name,sender_message,create_date  
	*/
	function deleteBabyItem($conn,$arr_input){
		$str_query = "delete from";
		$str_query .= " baby_item";
		$str_query .= " where";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$str_query .= " and item_id={$arr_input['item_id']}";
		if(isset($arr_input['seq'])){
			$str_query .= " and seq={$arr_input['seq']}";
		}
		
		$conn->executeQuery($str_query);
	}
	
	/* 获得礼品 
	* @param string $limit 
	* @prame: conn 数据库连接类
	* @prame: array $arr_input - baby_id,item_id,seq,get_type,sender_id,sender_name,sender_message,create_date  
	* @return : $arr_output
    *			baby_id, 			[0]<br>
    *			item_id, 		    [1]<br>
    *			seq, 			    [2]<br>
    *			get_type, 		    [3]<br>
    *			sender_id, 		    [4]<br>
    *			sender_name, 		[5]<br>
    *			sender_message, 	[6]<br>
    *			create_date, 		[7]<br>
	*/
	function getBabyItem($conn,$arr_input,$limit = null){
		$str_query = "select";
		if(isset($arr_input['cnt'])){
			$str_query .= " count(1)";
		}else{
			$str_query .= " baby_id";
			$str_query .= ",item_id";
            $str_query .= ",seq";
            $str_query .= ",get_type";
            $str_query .= ",sender_id";
            $str_query .= ",sender_name";
            $str_query .= ",sender_message";
            $str_query .= ",create_date";
		}
		$str_query .= " from";
		$str_query .= " baby_item";
		$str_query .= " where";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$arr_input .= " and get_type={$arr_input['get_type']}";
		
		if(isset($arr_input['item_id'])){
			$arr_input .= " and item_id={$arr_input['item_id']}";
		}
		if(isset($arr_input['seq'])){
			$str_query .= " and seq={$arr_input['seq']}";
		}
		if(isset($arr_input['orderby'])){
			$str_query .= " order by {$arr_input['orderby']}";
		}
		if($limit != null){
			$str_query .= " {$limit}";
		}
		
		$arr_output = $conn->selectQuery($str_query);
		return $arr_output;
	}
}

?>